IF(NOT MD5_NAMESPACE)
  SET(MD5_NAMESPACE "MD5")
  SET(MD5_STANDALONE 1)
ENDIF(NOT MD5_NAMESPACE)
# In all cases:
STRING(TOLOWER ${MD5_NAMESPACE} MD5_LIBRARY_NAME)

PROJECT(${MD5_NAMESPACE} C)
CMAKE_MINIMUM_REQUIRED(VERSION 2.2)

# Do full dependency headers.
INCLUDE_REGULAR_EXPRESSION("^.*$")

SET(MD5_SOURCES
  md5.c
  )

IF (WIN32)
  IF (BUILD_SHARED_LIBS)
    SET(MD5DLL 1)
  ELSE (BUILD_SHARED_LIBS)
    SET(MD5STATIC 1)
  ENDIF (BUILD_SHARED_LIBS)
ENDIF (WIN32)

# TODO
# Optimized function needs to define:
# ARCH_IS_BIG_ENDIAN
# Big endian thing:
#INCLUDE (${CMAKE_ROOT}/Modules/TestBigEndian.cmake)
#TEST_BIG_ENDIAN(MD5_WORDS_BIGENDIAN)
#SET_SOURCE_FILES_PROPERTIES(${MD5_SOURCES}
#  PROPERTIES COMPILE_FLAGS -DARCH_IS_BIG_ENDIAN
#  )
SET(MANGLE_PREFIX ${MD5_LIBRARY_NAME})
CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/md5_mangle.h.in
               ${CMAKE_CURRENT_BINARY_DIR}/md5_mangle.h
               @ONLY IMMEDIATE)

# for md5_mangle.h
INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR})

ADD_LIBRARY(${MD5_LIBRARY_NAME} ${MD5_SOURCES})
SET_TARGET_PROPERTIES(${MD5_LIBRARY_NAME} PROPERTIES ${GDCM_LIBRARY_PROPERTIES})
IF(NOT MD5_INSTALL_NO_LIBRARIES)
  INSTALL(TARGETS ${MD5_LIBRARY_NAME}
    RUNTIME DESTINATION ${MD5_INSTALL_BIN_DIR} COMPONENT Runtime
    LIBRARY DESTINATION ${MD5_INSTALL_LIB_DIR} COMPONENT Runtime
    ARCHIVE DESTINATION ${MD5_INSTALL_LIB_DIR} COMPONENT Development
  ${CPACK_NAMELINK_TYPE}
  )
ENDIF(NOT MD5_INSTALL_NO_LIBRARIES)

IF(MD5_STANDALONE)
  # md5main
  ADD_EXECUTABLE(md5main md5main.c)
  TARGET_LINK_LIBRARIES(md5main ${MD5_LIBRARY_NAME})
  IF(UNIX)
    TARGET_LINK_LIBRARIES(md5main -lm)
  ENDIF(UNIX)
  # md5cmp
  ADD_EXECUTABLE(md5cmp md5cmp.c)
  TARGET_LINK_LIBRARIES(md5cmp ${MD5_LIBRARY_NAME})
  # tst2md5
  ADD_EXECUTABLE(tst2md5 tst2md5.c)
  TARGET_LINK_LIBRARIES(tst2md5 ${MD5_LIBRARY_NAME})
  INSTALL(TARGETS md5main md5cmp tst2md5
    RUNTIME DESTINATION ${MD5_INSTALL_BIN_DIR} COMPONENT Runtime
    LIBRARY DESTINATION ${MD5_INSTALL_LIB_DIR} COMPONENT Runtime
    ARCHIVE DESTINATION ${MD5_INSTALL_LIB_DIR} COMPONENT Development
  ${CPACK_NAMELINK_TYPE}
  )

ENDIF(MD5_STANDALONE)



